package supermarket;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.commons.logging.LogFactory;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class ProductScraperCarrefour extends ProductScraper<Object, Object, Object> {

	public ProductScraperCarrefour(ProductManager manager) {
		super(manager);
		// TODO Auto-generated constructor stub
	}
	
	public static void main(String[] args){
		LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); 
		ProductScraperCarrefour psc = new ProductScraperCarrefour(null);
		psc.goToInitialPage();
	}

	@Override
	protected void getScrapeLinks(Object page) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected Object getNextProductCategory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected boolean hasNextProductCategory() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected Object goToInitialPage() {
		// TODO Auto-generated method stub
	    HtmlPage page = null;
		try {
			page = (HtmlPage) getWebClient().getPage(getMarketURL());
			HtmlInput shop = (HtmlInput) page.getElementById("rd_shop");
			HtmlPage pageAfterClick = shop.click();
			
		    // sleep 5s to wait for response of asynchronous call
		    try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			System.out.println(pageAfterClick.asXml());
			String inpHours = "inpHours-";
			String parts[] = pageAfterClick.asXml().split(inpHours);
			String id[] = parts[1].split("\"");
			System.out.println("id: " + id[0]);
			HtmlInput time = (HtmlInput) pageAfterClick.getElementById(inpHours + id[0]);
			pageAfterClick = time.click();

		    // sleep 5s to wait for response of asynchronous call
		    try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

			
			HtmlInput confirm = (HtmlInput) pageAfterClick.getElementById("submitPreHome");
			pageAfterClick = confirm.click();
			
		    // sleep 5s to wait for response of asynchronous call
		    try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println(pageAfterClick.asXml());
//		    HtmlAnchor anchor = page.getAnchorByHref("javascript:__doPostBack('linkNl','')");
//		    page = (HtmlPage) anchor.click();
		} catch (FailingHttpStatusCodeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void getProductCategories(Object page) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Object processProductCategory(Object category) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void getProducts(Object page) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean hasNextScrapeLinks() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object getNextScrapeLink() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void scrapeLink(Object link) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void scrapeProduct(Object product) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean hasNextProduct() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object getNextProduct() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Distributor getDistributor() {
		return Distributor.CARREFOUR;
	}

}
